//
//  3DZHIXUAN.m
//  EasyToo
//
//  Created by linshengqin on 13-6-24.
//
//

#import "3DZHIXUAN.h"
#import <AudioToolbox/AudioToolbox.h>
#import "DeviceUtil.h"
#import <QuartzCore/QuartzCore.h>
#import "CaiPiaoConfirmViewController.h"
#import "CaiPiaoLogic.h"
@implementation _DZHIXUAN
{
    int randArray[10];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:BackgroundColor];
    }
    return self;
}

/*
- (void)viewDidLoad
{
    [super viewDidLoad];    
    [self initLable];
    [self initButton];
	// Do any additional setup after loading the view.
    [self becomeFirstResponder]; // for shaking
}
*/
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self initView];
}

-(void) initView
{
    [self initData];
    [self initMainView];
    [self initButton];
    [self initLable];
}

-(void)initData
{
    caipiao_model = [CaiPiaoModel sharedata];
    [caipiao_model resetPara];
}

-(void)initMainView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
    _viewHundred = [[UIView alloc]initWithFrame:CGRectMake(20, 50, _bgScrollView.frame.size.width-40, 80)];
    _viewHundred.layer.borderColor = [UIColor grayColor].CGColor;
    _viewHundred.layer.borderWidth = 1 ;
    _viewHundred.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:_viewHundred];
    
    _viewTens = [[UIView alloc]initWithFrame:CGRectMake(20, _viewHundred.frame.origin.y+_viewHundred.frame.size.height+20, _bgScrollView.frame.size.width-40, 80)];
    _viewTens.layer.borderColor = [UIColor grayColor].CGColor;
    _viewTens.layer.borderWidth = 1 ;
    _viewTens.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:_viewTens];

    
    _viewSigle = [[UIView alloc]initWithFrame:CGRectMake(20, _viewTens.frame.origin.y+_viewTens.frame.size.height+20, _bgScrollView.frame.size.width-40, 80)];
    _viewSigle.layer.borderColor = [UIColor grayColor].CGColor;
    _viewSigle.layer.borderWidth = 1 ;
    _viewSigle.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:_viewSigle];

}
#define MAX_NUM 10
-(void) initButton
{
    int tag = 0;
    int num = 0;
    _btnArray = [[NSMutableArray alloc]init];
    _btnArray2 = [[NSMutableArray alloc]init];
    _btnArray3= [[NSMutableArray alloc]init];

    while (num<MAX_NUM)
    {
        m_button = [UIButton buttonWithType:UIButtonTypeCustom];
        m_button.frame = CGRectMake(70+(num%5)*40, (num/5)*40, 40, 40);
        m_button.tag=tag;
        // Set the font and color
        [m_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [m_button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        m_button.titleLabel.font = [UIFont boldSystemFontOfSize:10.0f];
        
        [m_button setTitle:[NSString stringWithFormat:@"%d",num] forState:UIControlStateNormal];
        
        [m_button setBackgroundColor:[UIColor clearColor]];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiu_bg"] forState:UIControlStateNormal ];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiured_bg"]forState:UIControlStateSelected];
        
        [m_button addTarget:self action:@selector(touristEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_btnArray addObject:m_button];
        [_viewHundred addSubview:m_button];
        num++;
        tag++;
    }
    num = 0;
    while (num<MAX_NUM)
    {
        m_button = [UIButton buttonWithType:UIButtonTypeCustom];
        m_button.frame = CGRectMake(70+(num%5)*40, (num/5)*40, 40, 40);
        m_button.tag=tag;
        // Set the font and color
        [m_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [m_button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        m_button.titleLabel.font = [UIFont boldSystemFontOfSize:10.0f];
        
        [m_button setTitle:[NSString stringWithFormat:@"%d",num] forState:UIControlStateNormal];
        
        [m_button setBackgroundColor:[UIColor clearColor]];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiu_bg"] forState:UIControlStateNormal ];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiured_bg"]forState:UIControlStateSelected];
        
        [m_button addTarget:self action:@selector(touristEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_btnArray2 addObject:m_button];
        [_viewTens addSubview:m_button];
        num++;
        tag++;
    }
    num = 0;
    while (num<MAX_NUM)
    {
        m_button = [UIButton buttonWithType:UIButtonTypeCustom];
        m_button.frame = CGRectMake(70+(num%5)*40, (num/5)*40, 40, 40);
        m_button.tag=tag;
        // Set the font and color
        [m_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [m_button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        m_button.titleLabel.font = [UIFont boldSystemFontOfSize:10.0f];
        
        [m_button setTitle:[NSString stringWithFormat:@"%d",num] forState:UIControlStateNormal];
        
        [m_button setBackgroundColor:[UIColor clearColor]];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiu_bg"] forState:UIControlStateNormal ];
        [m_button setBackgroundImage:[UIImage imageNamed:@"cp_ssqiured_bg"]forState:UIControlStateSelected];
        
        [m_button addTarget:self action:@selector(touristEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_btnArray3 addObject:m_button];
        [_viewSigle addSubview:m_button];
        num++;
        tag++;
    }
    
    UIButton* m_cleanall = [UIButton buttonWithType:UIButtonTypeCustom];
    m_cleanall.frame = CGRectMake(40,_viewSigle.frame.origin.y+_viewSigle.frame.size.height+20, 100, 40);
    [m_cleanall setTitle:[NSString stringWithFormat:@"%@",@"清空全部"] forState:UIControlStateNormal];
    [m_cleanall setBackgroundColor:[UIColor clearColor]];
    [m_cleanall setBackgroundImage:[UIImage imageNamed:@"clear_all"] forState:UIControlStateNormal ];
    [m_cleanall addTarget:self action:@selector(onClickCancle:) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:m_cleanall];
    
    UIButton* m_confirm = [UIButton buttonWithType:UIButtonTypeCustom];
    m_confirm.frame = CGRectMake([DeviceUtil getScreenWidth]/2+20, _viewSigle.frame.origin.y+_viewSigle.frame.size.height+20, 100, 40);
    [m_confirm setTitle:[NSString stringWithFormat:@"%@",@"下一步"] forState:UIControlStateNormal];
    [m_confirm setBackgroundColor:[UIColor clearColor]];
    [m_confirm setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal ];
    [m_confirm addTarget:self action:@selector(onClickConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:m_confirm];
}

-(void) initLable
{
    tips = [[[UILabel alloc] initWithFrame:CGRectMake(30, 10, 170, 30)]autorelease];
    tips.text =  @"至少选择一个号码";
    tips.font = [UIFont boldSystemFontOfSize:14.0f];  //UILabel的字体大小
    tips.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    [tips setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:tips];
    
    UIButton* tips_button = [UIButton buttonWithType:UIButtonTypeCustom];
    tips_button.frame = CGRectMake( 220, 10,70, 30);
    tips_button.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    
    [tips_button setTitle:[NSString stringWithFormat:@"%@",@"机选"] forState:UIControlStateNormal];
    [tips_button setBackgroundColor:[UIColor clearColor]];
    [tips_button setBackgroundImage:[UIImage imageNamed:@"cp_sshongq_bg"] forState:UIControlStateNormal ];
    [tips_button addTarget:self action:@selector(randBall:) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:tips_button];
    
    UILabel *tips3 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 40, 40)]autorelease];
    tips3.text =  @"百位";
    tips3.font = [UIFont boldSystemFontOfSize:14.0f];  //UILabel的字体大小
    tips3.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
    [tips3 setBackgroundColor:[UIColor clearColor]];

    [_viewHundred addSubview:tips3];
    
    
    UILabel *tips4 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 40, 40)]autorelease];
    tips4.text =  @"十位";
    tips4.font = [UIFont boldSystemFontOfSize:14.0f];  //UILabel的字体大小
    tips4.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
    [tips4 setBackgroundColor:[UIColor clearColor]];

    [_viewTens addSubview:tips4];
    
    UILabel *tips5 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 40, 40)]autorelease];
    tips5.text =  @"个位";
    tips5.font = [UIFont boldSystemFontOfSize:14.0f];  //UILabel的字体大小
    tips5.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
    [tips5 setBackgroundColor:[UIColor clearColor]];
    [_viewSigle addSubview:tips5];
}


-(void)randBall:(id)sender
{
    for (UIButton *button in _btnArray) {
        button.selected = NO;
    }
    for (UIButton *button in _btnArray2) {
        button.selected = NO;
    }
    for (UIButton *button in _btnArray3) {
        button.selected = NO;
    }
    for( int i=0;i<10;i++)
    {
        randArray[i]=i;
    }
    
    int x = arc4random() % 10;
    UIButton* btn = [_btnArray objectAtIndex:x];
    btn.selected = YES;
    
    x = arc4random() % 10;
    btn = [_btnArray2 objectAtIndex:x];
    btn.selected = YES;
    
    x = arc4random() % 10;
    btn = [_btnArray3 objectAtIndex:x];
    btn.selected = YES;
    [self touristEvent:nil];
}

-(void)touristEvent:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    
    int count = 0;
    int count2 = 0;
    int count3 = 0;
    for (UIButton *button in _btnArray) {
        if( button.selected == YES )
            count++;
    }
    for (UIButton *button in _btnArray2) {
        if( button.selected == YES )
            count2++;
    }
    for (UIButton *button in _btnArray3) {
        if( button.selected == YES )
            count3++;
    }
    
    int m_totalCount = count*count2*count3;
    int m_totalPrice = m_totalCount*2;
    if(totalPrice > 20000)
    {
        printf("不能超过20000元\n");
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"不能超过20000元" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] autorelease];
        [baseAlert show];
        button.selected = !button.selected;
    }
    else
    {
        totalCount = m_totalCount;
        totalPrice = m_totalPrice;
        self.fatherView.navigationItem.title = [NSString stringWithFormat:@"共%d注 %d元",totalCount,totalPrice];
        
    }
}


-(void)onClickCancle:(id)sender
{
    printf("cancle\n");
    for (UIButton *button in _btnArray) {
        button.selected = NO;
    }
    
    _toolbarLabel.text = [NSString stringWithFormat:@"共0注 0元"];
}

-(void)onClickConfirm:(id)sender
{
    printf("zhixuan confirm\n");
    int count = 0;
    int count2 = 0;
    int count3 = 0;
    for (UIButton *button in _btnArray) {
        if( button.selected == YES )
            count++;
    }
    for (UIButton *button in _btnArray2) {
        if( button.selected == YES )
            count2++;
    }
    for (UIButton *button in _btnArray3) {
        if( button.selected == YES )
            count3++;
    }
    //  _toolbarLabel.text  = @"共0注 0元";
    if( count<1 || count2<1 || count3< 1)
    {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"每位不少于一个球" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] autorelease];
        [baseAlert show];
        return;
    }
    
   
    
    if( [[CaiPiaoLogic shareInstance] cmpTime] != NSOrderedAscending )
    {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"投注时间已过" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] autorelease];
        [baseAlert show];
        return;
    }
    
    NSMutableString* sigleString = [[NSMutableString alloc]init];
    NSMutableString* tenString = [[NSMutableString alloc]init];
    NSMutableString* hundredString = [[NSMutableString alloc]init];
    int flag = 1;
    for (UIButton *button in _btnArray3)
    {
        if(button.selected)
        {
            if(flag)
            {
                [sigleString appendString:button.titleLabel.text];
                flag = 0;
            }
            else
            {
                [sigleString appendFormat:@",%@",button.titleLabel.text];
            }
            button.selected = NO;
        }
    }
    flag = 1;
    for (UIButton *button in _btnArray2)
    {
        if(button.selected)
        {
            if(flag)
            {
                [tenString appendString:button.titleLabel.text];
                flag = 0;
            }
            else
            {
                [tenString appendFormat:@",%@",button.titleLabel.text];
            }
            button.selected = NO;
        }
    }
    flag = 1;
    for (UIButton *button in _btnArray)
    {
        if(button.selected)
        {
            if(flag)
            {
                [hundredString appendString:button.titleLabel.text];
                flag = 0;
            }
            else
            {
                [hundredString appendFormat:@",%@",button.titleLabel.text];
            }
            button.selected = NO;
        }
    }
    NSString* result = [NSString stringWithFormat:@"%@|%@|%@",hundredString,tenString,sigleString];
    [caipiao_model.stakeList addObject:result];
    [caipiao_model.listNum addObject:[NSNumber numberWithLong:totalCount]];
    [caipiao_model.listPrice addObject:[NSNumber numberWithLong:totalPrice]];
    caipiao_model.lType = 2;
    CaiPiaoConfirmViewController  *confirm = [[[CaiPiaoConfirmViewController alloc]init]autorelease];
    [self.fatherView.navigationController pushViewController:confirm animated:YES];
}



#pragma mark shark delegate

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //检测到摇动
    printf("shaking begin!\n");
    
}



- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //摇动取消
    
}



- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //摇动结束
    
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        
        //something happens
        printf("shaking end!\n");
        for (UIButton *button in _btnArray)
        {
            button.selected = NO;
        }
        for (UIButton *button in _btnArray2)
        {
            button.selected = NO;
        }
        for (UIButton *button in _btnArray3)
        {
            button.selected = NO;
        }
        
        _toolbarLabel.text = [NSString stringWithFormat:@"共%d注 %d元",0,0];
        for( int i=0;i<10;i++)
        {
            randArray[i]=i;
        }
        
        int x = arc4random() % 10;
        UIButton* btn = [_btnArray objectAtIndex:x];
        btn.selected = YES;
        
        x = arc4random() % 10;
        btn = [_btnArray2 objectAtIndex:x];
        btn.selected = YES;
        
        x = arc4random() % 10;
        btn = [_btnArray3 objectAtIndex:x];
        btn.selected = YES;
        [self randBall:nil];
        [self touristEvent:nil];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    
}


@end
